<?php
/**
* Author:rui
* Email:zer0131@vip.qq.com
* 商品模型
*/
class GoodsModel extends CommonModel{
	protected $_validate = array(
		array('category','require','请选择商品分类'),
		array('title','require','标题不能为空'),
		array('nowprice','require','请填写现价'),
		array('oldprice','require','请填写原价'),
		array('starttime','require','请填写开始时间'),
		array('endtime','require','请填写结束时间'),
		array('content','require','详情不能为空'),
		array('nowprice','currency','现价格式不正确',2),
		array('oldprice','currency','原价格式不正确',2),
		array('attention','number','关注人数格式不正确',2),
		array('nowprice,oldprice','checkPrice','现价不能大于原价',1,'callback',3),
		array('starttime,endtime','checkTime','开始时间不能大于结束时间',1,'callback',3),

	);

	protected $_auto = array(
		array('addtime','time',1,'function'),
		array('starttime','strtotime',3,'function'),
		array('endtime','strtotime',3,'function'),
		array('attention','randAttention',1,'callback'),
	);

	//判断价格
	protected function checkPrice($data){
		if($data['nowprice'] > $data['oldprice']){
			return false;
		}
		else{
			return true;
		}
	}

	//时间判断
	protected function checkTime($data){
		$start = strtotime($data['starttime']);
		$end = strtotime($data['endtime']);
		if($start > $end){
			return false;
		}
		else{
			return true;
		}
	}

	//产生随机的关注人数
	protected function randAttention($data){
		if(empty($data)){
			$data = mt_rand(1000,9999);
		}
		return $data;
	}
}
?>